#include "recorditem.h"
#include "ui_recorditem.h"
#include "eventcenter.h"
#include "UIEditor/uilib.h"
#include <QDate>
#include "Narration/narrationnode.h"
#include "GameData/jsontranslator.h"
#include "ResourceManger/filemanager.h"
#include "gamemanager.h"
#include <QTextEdit>
RecordItem::RecordItem(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RecordItem)
{
    ui->setupUi(this);
    Init();
}

RecordItem::~RecordItem()
{
    delete ui;
}
void RecordItem::Refresh(int id_)
{
    narrationId = id_;
    NarrationNode* node = NarrationTree::Instance().narrationTree[narrationId];
    if(!node)
        return;
    time = QString::number(QDate::currentDate().year()) + " " +
           QString::number(QDate::currentDate().month()) + " " +
           QString::number(QDate::currentDate().day()) + " " +
           QTime::currentTime().toString();
    ui->deleteRecordBtn->show();
    ui->label->Refresh(UILib::Instance().labels["存档-存档框"]);
    QString name = node->name,text = node->text;
    QTextEdit * edit = new QTextEdit();
    edit->setText(name);
    name = edit->toPlainText();
    edit->setText(text);
    text = edit->toPlainText();
    delete edit;
    edit = nullptr;
    ui->label->setText("存档\n" + time + "\n\n" + name + '\n' + text);
}

void RecordItem::mousePressEvent(QMouseEvent *event)
{
    QWidget::mousePressEvent(event);
    if(empty)
    {
        //找到根节点
        NarrationNode* root = GameManager::Instance().curNode;
        while(root && NarrationTree::Instance().narrationTree[root->parentId])
        {
            root = NarrationTree::Instance().narrationTree[root->parentId];
        }
        Refresh(root->id);
        emit EventCenter::Instance().AddRecord();
        empty = false;
    }
    else
    {
//        GameManager::Instance().SetRecord(this);
//        emit EventCenter::Instance().RunRecord(this);
        ui->label->hide();
        ui->BtnWidget->show();
    }
}

void RecordItem::Init()
{
    //外观
    ui->label->SetSelectedDisabled(true);
    ui->label->Refresh(UILib::Instance().labels["存档-添加存档"]);
    ui->label->setWordWrap(true);
    ui->label->setFixedSize(600,600);
    Refresh(narrationId);
    ui->deleteRecordBtn->Refresh(UILib::Instance().btns["存档-删除存档"]);
    ui->deleteRecordBtn->SetSelectedDisabled(true);
    ui->BtnWidget->hide();
    Refresh(narrationId);
    connect(ui->deleteRecordBtn,&QPushButton::clicked,this,[=](){
        emit EventCenter::Instance().DeleteRecord(id);
    });
    connect(ui->BackBtn,&QPushButton::clicked,this,[=](){
        ui->BtnWidget->hide();
        ui->label->show();
    });
    connect(ui->RunBtn,&QPushButton::clicked,this,[=](){
        GameManager::Instance().SetRecord(this);
        emit EventCenter::Instance().RunRecord(this);
    });
}

void RecordItem::Save()
{
    //数值
    JsonTranslator jt;
    data = jt.ToJson();
    //数据持久化
    QString path = FileManager::Instance().recordPath;
    QJsonObject* ob = jt.ToJson(this);
    QJsonObject* recordOb = FileManager::Instance().ReadJsonFile(path);
    recordOb->remove(QString::number(id));
    recordOb->insert(QString::number(id),*ob);
    FileManager::Instance().Write(path,jt.Json2Byte(recordOb));
}

void RecordItem::Switch(bool btn)
{
    if(btn)
    {
        ui->BtnWidget->show();
        ui->label->hide();
    }else
    {
        ui->label->show();
        ui->BtnWidget->hide();
    }
}
